#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int m, n, g[N][N];
int stop[N], dist[N];

void bfs() {
  queue<int> q;
  memset(dist, -1, sizeof dist);
  q.push(1);
  dist[1] = 0;
  while (q.size()) {
    int t = q.front();
    q.pop();
    for (int i = 1; i <= n; ++i) {
      if (dist[i] != -1) continue;
      if (!g[t][i]) continue;
      dist[i] = dist[t] + 1;
      q.push(i);
    }
  }
}
int main() {
  cin >> m >> n;
  while (m--) {
    int num = 0;
    for (int i = 0;; i++) {
      char ch;
      cin >> stop[i];
      num++;
      ch = cin.get();
      if (ch == '\n') break;
    }
    for (int i = 0; i < num - 1; ++i) {
      for (int j = i + 1; j < num; ++j) {
        g[stop[i]][stop[j]] = true;
      }
    }
  }
  bfs();
  if (dist[n] == -1)
    cout << "NO\n";
  else
    cout << dist[n] - 1 << '\n';
}
